package com.tjsinfo.tjsoa.system.bo;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.IMaximDAO;
import com.tjsinfo.tjsoa.system.vo.Maxim;

public class MaximBO implements IMaximBO {
	private IMaximDAO maximDao;
	
	public void setMaximDao(IMaximDAO maximDao){
		this.maximDao = maximDao;
	}
	private IQbcDao qbcDao;

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public void DelMaxim(Integer id) {
		Maxim maxim=(Maxim)qbcDao.getObjectById(Maxim.class, id);
		maximDao.delete(maxim);
	}
	public void SaveMaxim(Maxim maxim) {
		DetachedCriteria criteriaAll = DetachedCriteria.forClass(Maxim.class);
		criteriaAll.addOrder(Order.asc("id"));
		List<Maxim> maximList=qbcDao.getCriteria(criteriaAll);
		if(maximList.isEmpty()){
			maxim.setId(Integer.valueOf(1));
			maximDao.save(maxim);
		}else{
			int i=1;
			for (Maxim maxim2 : maximList) {//补充当前数据库序列
				if(maxim2.getId().intValue()!=i){
					maxim.setId(i);
					break;
				}
				i++;
			}
			if(maxim.getId()==null)maxim.setId(i);//数据库排序正常,在最后补充
			maximDao.save(maxim);
		}
		
	}
	
	public Maxim getRandomMaxim() {
		DetachedCriteria criteriaAll = DetachedCriteria.forClass(Maxim.class);
		criteriaAll.addOrder(Order.asc("id"));
		List<Maxim> maximList=qbcDao.getCriteria(criteriaAll);
		int count=maximList.size();
		//问候语
		String id="1";
		int c=1;
		Maxim rmi=null;
		if(maximList.isEmpty()){
			rmi=new Maxim();
			rmi.setTitle("欢迎使用TJSOA办公软件");
		}
		while (rmi==null && c<10) {
			Double ran=Math.random()*count;
			id=String.valueOf(ran.intValue());
			rmi=(Maxim)maximList.get(Integer.valueOf(id));
			c++;
		}
		if(rmi==null){
			rmi.setTitle("欢迎使用TJSOA办公软件");
		}
		return rmi;
	}
}
